/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.yokeyword.sample.slice;

import me.yokeyword.sample.ResourceTable;
import me.yokeyword.sample.demo_flow.MainAbility;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Button;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

public class EnterAbilitySlice extends AbilitySlice {
    private ComponentContainer rootLayout;

    private Button mButtonFlow;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        rootLayout = (ComponentContainer) LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_ability_enter, null, false);
        initViews(rootLayout);
        initViewClick();
        super.setUIContent(rootLayout);
    }

    private void initViews(ComponentContainer rootLayout) {
        mButtonFlow = (Button) rootLayout.findComponentById(ResourceTable.Id_btn_flow);
        ShapeElement background = new ShapeElement();
        background.setShape(ShapeElement.RECTANGLE);
        background.setRgbColor(new RgbColor(163, 194, 194));
        mButtonFlow.setBackground(background);
        mButtonFlow.setTextColor(Color.BLACK);
    }

    private void initViewClick() {
        mButtonFlow.setClickedListener(component -> {
            Operation operation = new Intent.OperationBuilder()
                    .withBundleName("me.yokeyword.sample")
                    .withAbilityName("demo_flow."+ MainAbility.class.getSimpleName())
                    .build();
            Intent intent = new Intent();
            intent.setOperation(operation);
            startAbility(intent);
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
